package com.gdupt.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gdupt.entity.CustomerContact;
import com.gdupt.entity.vo.ResponseData;
import com.gdupt.service.ICustomerContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 客户联系人 客户联系人 前端控制器
 * </p>
 *
 * @author gdupt
 * @since 2018-11-21
 */
@Controller
@RequestMapping("/customer-contact")
public class CustomerContactController {

    @Autowired
    private ICustomerContactService customerContactService;

    @RequestMapping("/list/{customerId}")
    public @ResponseBody ResponseData list(Page page, @PathVariable(value = "customerId") Long customerId){
        List<CustomerContact> list = customerContactService.search(page,customerId);
        return ResponseData.okPage(page.getTotal(),list);
    }

    @PostMapping("/save")
    public @ResponseBody ResponseData save(CustomerContact customerContact){
        boolean flg = customerContactService.saveOrUpdate(customerContact);
        if (flg){
            return ResponseData.okMsg("添加成功");
        }
        return ResponseData.failMsg("添加失败");
    }

    @PostMapping("/del/{id}")
    public @ResponseBody ResponseData del(@PathVariable("id") Integer id){
        boolean flg = customerContactService.removeById(id);
        if (flg){
            return ResponseData.okMsg("删除成功");
        }
        return ResponseData.failMsg("删除失败");
    }

    @PostMapping("/edit")
    public ResponseData edit(CustomerContact customerContact){
        boolean flg = customerContactService.saveOrUpdate(customerContact);
        if (flg){
            return ResponseData.okMsg("编辑成功");
        }
        return ResponseData.failMsg("编辑失败");
    }

    @RequestMapping("/editPage/{id}")
    public String editPage(@PathVariable("id") Integer id,Model model){
        CustomerContact contact = customerContactService.getById(id);
        model.addAttribute("contact",contact);
        return "customerContact/edit";//获取数据并跳转到views/customerContact/edit.jsp
    }

}
